﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using Gift.Model;
using Gift.DAL;
using Gift.WebApp;

namespace WebApp.admin.warehouse
{
    public partial class cardGoodsOut : BaseAdmin
    {
    
        tbGoods oGoods;
        tbWarehouseGoodsOut oWarehouseGoodsOut;
        int iGoodsOutCount = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                base.ValidateLoginRule(Gift.Model.ENUM.ERule.WarehouseGoodsManage);
                this.BuildForm();
                this.BindWarehouse();
              
            }
        }

        void BindGoodsBatchNoList()
        {
            if (this.ddlWarehouse.SelectedItem.Value != "")
            {
                string sWhere = string.Format(" goodsId = {0} and warehouseId = {1} "
                                               , Request.QueryString["gid"]
                                               , this.ddlWarehouse.SelectedItem.Value);
                DataTable tbBatch = BaseFactory.theWarehouseGoods.GetList(sWhere).Tables[0];
                this.ddlGoodsBatchNo.DataTextField = "goodsBatchNo";
                this.ddlGoodsBatchNo.DataValueField = "goodsBatchNo";
                this.ddlGoodsBatchNo.DataSource = tbBatch;
                this.ddlGoodsBatchNo.DataBind();
            }

        }

        void BuildForm()
        {
            oGoods = BaseFactory.theGoods.GetModel(int.Parse(Request.QueryString["gid"]));
            this.lblProName.Text = oGoods.goodsName;
            DataTable dt = BaseFactory.theWarehouseGoodsOut.GetList(string.Format(" companyId={0} and orderId={1} and goodsId={2}"
                                                                        , base.Administrator.CompanyId
                                                                        , Request.QueryString["oid"]
                                                                        , Request.QueryString["gid"]
                                                                        )).Tables[0];

            StringBuilder sbDescription = new StringBuilder();
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    sbDescription.AppendFormat("库房：{0},出库数量：{1},"
                                                , BaseFactory.theWarehouse.GetModel(int.Parse(dr["warehouseId"].ToString()))
                                                , dr["goodsCount"].ToString()
                                                );
                    iGoodsOutCount = iGoodsOutCount + int.Parse(dr["goodsCount"].ToString());
                }
                ViewState["goodsOutCount"] = iGoodsOutCount;
            }
            int iOutCount = 0;
            if (ViewState["goodsOutCount"] != null)
            {
                iOutCount = int.Parse(Request.QueryString["gcount"]) - int.Parse(ViewState["goodsOutCount"].ToString());
            }
            else
            {
                iOutCount = int.Parse(Request.QueryString["gcount"]);
            }
            sbDescription.AppendFormat("您还需要出库商品{0}件。", iOutCount);
            this.lblOutDescription.Text = sbDescription.ToString();
            this.txtGoodsCount.Value = iOutCount.ToString();
        }

        void BindWarehouse()
        {
            DataTable dt = BaseFactory.theWarehouse.GetWarehouseGoodsList(int.Parse(base.Administrator.CompanyId), int.Parse(Request.QueryString["gid"])).Tables[0];
            if (dt != null)
            {
                this.ddlWarehouse.DataTextField = "warehouseGoodsName";
                this.ddlWarehouse.DataValueField = "id";
                this.ddlWarehouse.DataSource = dt;
                this.ddlWarehouse.DataBind();
            }
            this.ddlWarehouse.Items.Insert(0, new ListItem("请选择仓库", ""));

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            int iOutCount = 0;
            if (ViewState["goodsOutCount"] != null)
            {
                iOutCount = int.Parse(Request.QueryString["gcount"]) - int.Parse(ViewState["goodsOutCount"].ToString());
            }
            else
            {
                iOutCount = int.Parse(Request.QueryString["gcount"]);
            }
            if (iOutCount >= int.Parse(this.txtGoodsCount.Value))
            {
                oWarehouseGoodsOut = new tbWarehouseGoodsOut();
                oWarehouseGoodsOut.companyId = int.Parse(base.Administrator.CompanyId);
                oWarehouseGoodsOut.creater = int.Parse(base.Administrator.AdminUserId);
                oWarehouseGoodsOut.createTime = DateTime.Now;
                oWarehouseGoodsOut.goodsCount = int.Parse(this.txtGoodsCount.Value);
                oWarehouseGoodsOut.goodsId = int.Parse(Request.QueryString["gid"]);
                oWarehouseGoodsOut.orderId = long.Parse(Request.QueryString["oid"]);
                oWarehouseGoodsOut.warehouseId = int.Parse(this.ddlWarehouse.SelectedItem.Value);
                oWarehouseGoodsOut.GoodsBatchNo = this.ddlGoodsBatchNo.SelectedItem.Value;
                oWarehouseGoodsOut.remark = this.txtRemark.Text.Trim();
                if (BaseFactory.theWarehouseGoodsOut.Add(oWarehouseGoodsOut) > 0)
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"出库成功！\");  window.opener.document.location.reload();window.opener=null;window.open('','_self');window.close();</script>");    
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"出库失败！\");</script>");
                }
            }
            else
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"商品出库数量不能大于订单商品数量！\");</script>");
            }


        }

        protected void ddlWarehouse_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BindGoodsBatchNoList();
        }
    }
}